import { _decorator, Component, Label, Node, randomRangeInt, Sprite, SpriteFrame, tween, Vec3 } from 'cc';
import { GameDataManager } from '../GameDataManager';
import { EasingType } from 'db://assets/Scripts/Framework/Utils/TweenUtil';
import { MainUI } from './MainUI';
import { BundleManager } from 'db://assets/Scripts/Framework/Managers/BundleManager';
import { ProjectEventManager, ProjectEvent } from 'db://assets/Scripts/Framework/Managers/ProjectEventManager';
const { ccclass, property } = _decorator;

@ccclass('SignUI')
export class SignUI extends Component {

    @property([Node])
    items: Node[] = [];

    protected onEnable(): void {
        tween(this.node.children[1])
            .to(0.5, { scale: Vec3.ONE }, { easing: EasingType.backOut })
            .start();
        this._CheckDaily();
        ProjectEventManager.emit(ProjectEvent.弹出窗口);
    }

    start() {

    }

    update(deltaTime: number) {

    }

    Back() {
        tween(this.node.children[1])
            .to(0.5, { scale: Vec3.ZERO }, { easing: EasingType.backIn })
            .call(() => { this.node.active = false; })
            .start();
        MainUI.Instance.PlayClick();
    }

    _CheckDaily() {
        let daily = GameDataManager.Instance.getNumberData("Daily");
        let day = daily % 7;
        let lastDaily = GameDataManager.Instance.getArrayData("LastDaily");
        let date = new Date;
        let isDaily = false;
        if (lastDaily.length > 0 && lastDaily[0] == date.getFullYear() && lastDaily[1] == date.getMonth() && lastDaily[2] == date.getDate()) isDaily = true;
        for (let i = 0; i < this.items.length; i++) {
            const element = this.items[i];
            if (i < 6 && day > i || day == 0 && isDaily) {
                element.children[0].active = false;
                element.children[element.children.length - 1].active = true;
            }
            else {
                element.children[0].active = day == i && !isDaily;
                element.children[element.children.length - 1].active = false;
            }
        }
    }

    DailyButton() {
        let lastDaily = GameDataManager.Instance.getArrayData("LastDaily");
        let date = new Date;
        if (lastDaily[0] == date.getFullYear() && lastDaily[1] == date.getMonth() && lastDaily[2] == date.getDate()) return;
        lastDaily[0] = date.getFullYear();
        lastDaily[1] = date.getMonth();
        lastDaily[2] = date.getDate();
        GameDataManager.Instance.setArrayData("LastDaily", lastDaily);
        let daily = GameDataManager.Instance.getNumberData("Daily");
        daily++;
        GameDataManager.Instance.setNumberData("Daily", daily);
        let day = daily % 7;
        day = day == 0 ? 6 : day - 1;
        let delta = 5;
        let num = 2;
        let label = MainUI.Instance.finish.children[3].getComponent(Label);
        if (day < 3) {
            num = 0;
            let coin = GameDataManager.Instance.getNumberData("Coin");
            delta = 100 + 200 * day;
            coin += delta;
            GameDataManager.Instance.setNumberData("Coin", coin);
            label.string = `金币x${delta}`;
        }
        else if (day < 6) {
            num = 1;
            let diamond = GameDataManager.Instance.getNumberData("Diamond");
            delta = 100 + 50 * day;
            diamond += delta;
            GameDataManager.Instance.setNumberData("Diamond", diamond);
            label.string = `钻石x${delta}`;
        }
        else {
            let skinPieces = GameDataManager.Instance.getArrayData<number[]>("SkinPieces");
            let type = randomRangeInt(0, skinPieces.length);
            let num = randomRangeInt(0, skinPieces[type].length);
            skinPieces[type][num]++;
            GameDataManager.Instance.setArrayData("SkinPieces", skinPieces);
            let data = MainUI.Instance.items.Adorn[type][num];
            let sprite = MainUI.Instance.finish.children[2].children[2].children[0].getComponent(Sprite);
            BundleManager.GetBundle("MainPack").load(data.spriteFrame, SpriteFrame, (err, data) => { sprite.spriteFrame = data; });
            label.string = `${data.name}碎片x${delta}`;
        }
        MainUI.Instance._InitToken();
        for (let i = 0; i < MainUI.Instance.finish.children[2].children.length; i++) {
            const element = MainUI.Instance.finish.children[2].children[i];
            element.active = i == num;
        }
        tween(MainUI.Instance.finish)
            .to(0.5, { scale: Vec3.ONE }, { easing: EasingType.backOut })
            .start();
        this._CheckDaily();
        MainUI.Instance._InitRed();
        MainUI.Instance.PlayBonus();
    }
}


